package com.codi.api.controller.bank;

import com.codi.api.security.UserUtil;
import com.codi.api.validation.AccountValidation;
import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.MD5;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.core.domain.BankName;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.service.BankService;
import com.codi.bus.core.service.BranchBankService;
import com.codi.bus.core.service.QueryService;
import com.codi.fcloud.ufx.hssale.acco.ClearTradePassword;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by asus on 2018/2/28.
 */
@Slf4j
@Controller
@RestController
@RequestMapping("/branchBank")
public class BranchBankController {

    @Autowired
    private BranchBankService branchBankService;

    @Autowired
    private AccountValidation accountValidation;

    @Autowired
    private QueryService queryService;
    /**
     * 省份
     */
    @GetMapping("province")
    @Loggable
    public BaseResult getProvince(){
        List<String> provinces = branchBankService.getProvince();
        BaseResult result = new BaseResult();
        result.setResult(provinces);
        return result;
    }

    /**
     * 地级市
     */
    @GetMapping("city")
    @Loggable
    public BaseResult getCityByProvince(String provinceName){
        List<String> citys = branchBankService.getCity(provinceName);
        BaseResult result = new BaseResult();
        result.setResult(citys);
        return result;
    }

    /**
     * 一个市的一种银行
     */
    @GetMapping("cityBank")
    @Loggable
    public BaseResult getBankByCity(String cityName, String bankName){
        List<BankName> cityBanks = branchBankService.getCityBank(cityName, bankName);
        BaseResult result = new BaseResult();
        result.setResult(cityBanks);
        return result;
    }

    /**
     * 搜索支行
     */
    @GetMapping("fundBank")
    @Loggable
    public BaseResult getBankByWord(String bankName, String word){
        List<BankName> fundBanks = branchBankService.getFundBank(bankName, word);
        BaseResult result = new BaseResult();
        result.setResult(fundBanks);
        return result;
    }

    /**
     * 支行修改
     */
    @PostMapping("setBank")
    @Loggable
    public BaseResult setBranchBank( String vcBranchbank, String vcBankname, String tradePassword){
        BaseResult result = null;
        result = accountValidation.setBranchBankValidate(vcBranchbank,tradePassword);
        if (result != null){
            return result;
        }
        Long userId = UserUtil.getLoginUserId();
        return branchBankService.setBranchBank(userId, vcBranchbank, vcBankname, tradePassword);
    }


    @GetMapping("test")
    public BaseResult testA() {
//        BaseResult result = queryService.checkSharesExistByBankAccount(4206L, "6222021257558459888");
//        queryService.queryAssets(4206L);

        clearTradePassword.clearTradePasswd("0724", MD5.MD5Encode("654321"));
        return null;
    }


    @Autowired
    private BankService bankService;

    @Resource(name = "clearTradePassword")
    private ClearTradePassword clearTradePassword;
}
